//
//  BaseBridgeModule.m
//  eCourseApp
//
//  Created by andrew on 2018/8/10.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "BaseBridgeModule.h"
#import "ECourseConstant.h"

#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>

@implementation BaseBridgeModule

@synthesize bridge = _bridge;

RCT_EXPORT_METHOD(showPackageName:(NSString *)msg
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){
  resolve(msg);
}

RCT_EXPORT_METHOD(backToExue){
  dispatch_async(dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"backToLogin" object:nil userInfo:nil];
  });
}

RCT_EXPORT_METHOD(playVideo:(NSString *)videoUrl) {
  dispatch_async(dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playViedo" object:nil userInfo:@{@"playUrl":videoUrl}];
  });
}

RCT_EXPORT_METHOD(globalSingleton:
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){
  
  ECourseConstant *eCourseConstant = [ECourseConstant sharedECourseConstant];
  
  NSMutableDictionary *res = [NSMutableDictionary dictionary];
  
  NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  [userInfo setObject:eCourseConstant.userInfo.uuid forKey:@"uuid"];
  [userInfo setObject:eCourseConstant.userInfo.loginType forKey:@"loginType"];
  [userInfo setObject:eCourseConstant.userInfo.token forKey:@"token"];
  
  [res setObject:userInfo forKey:@"userInfo"];
  [res setObject:@"" forKey:@"eCourseFirstRouter"];
  [res setObject:@{} forKey:@"map"];
  
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:res
                                                     options:NSJSONWritingPrettyPrinted
                                                       error:nil];
  NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  
  resolve(str);
  
}

// 设置支付密码
RCT_EXPORT_METHOD(setPayPassword){
  dispatch_async(dispatch_get_main_queue(), ^{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [self.bridge.eventDispatcher sendDeviceEventWithName:@"PassWordSetted_NativeEmitter" body:nil];
    });
  });
}

RCT_EXPORT_METHOD(refreshToken){
  dispatch_async(dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"saveBridge" object:nil userInfo:@{@"bridge": self.bridge}];
  });
}

RCT_EXPORT_METHOD(ejectPayPanel:(NSString *)money){
  dispatch_async(dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ejectPayPanel" object:nil userInfo:@{@"money": money}];
  });
}

RCT_EXPORT_MODULE(BaseBridge);

@end



